package Year22_Month07.day02;



public class 把字符串转化为整数 {
    public static int StrToInt(String str) {
        char[] arr = str.toCharArray();
        int num = 0;
        if(arr.length == 1 && str.equals("0")) return 0;
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            if(arr[0] == '+' && i == 0) {
                flag = false;
                continue;
            }
            if(arr[0] == '-' && i == 0) {
                flag = true;
                continue;
            }
            if(arr[i] < '0' || arr[i] > '9'){
                return 0;
            }
            num = num * 10 + arr[i] - '0';
        }
        if(flag) return -num;
        return num;
    }

    public static void main(String[] args) {
        System.out.println(StrToInt("+2147483647"));
    }

}
